package com.psytech.app .task.web_ui.server;

import java.util.logging.Level;
import java.util.logging.Logger;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.XMLConfiguration;

public class Initializer implements ServletContextListener {

	public static XMLConfiguration CONF;

	private static Logger LOG = Logger.getLogger(Initializer.class.getName());

	@Override
	public void contextDestroyed(final ServletContextEvent arg0) {
		LOG.info("Arrêt de TaskMan webui");
	}

	@Override
	public void contextInitialized(final ServletContextEvent arg0) {
		String configurationFile = null;
		try {
			final Context ctx = new InitialContext();
			configurationFile=(String) ctx.lookup( "java:comp/env/taskmanager.web-ui.properties.configFile" );
			LOG.log(Level.INFO,"Par environement entry : "+configurationFile);
		} catch (final NamingException e1) {
			LOG.log(Level.INFO,"",e1);
		}
		if ((configurationFile==null) && (System.getProperty("taskmanager.web-ui.properties.configFile") != null)){
			configurationFile = System.getProperty("taskmanager.web-ui.properties.configFile");
			LOG.log(Level.INFO,"Par property : "+configurationFile);
		}
		else if(configurationFile==null)
			configurationFile = "../taskmanager.web-ui.xml";
		try {
			CONF=new XMLConfiguration(configurationFile);

		} catch (final ConfigurationException e) {
			System.err.println(e.getMessage());
			throw new RuntimeException(e);
		}


		LOG.info("Démarrage de TaskMan webui");
	}

}
